from django.shortcuts import render

# Create your views here.
from rest_framework.filters import OrderingFilter
from rest_framework.generics import ListAPIView
from rest_framework.response import Response

from rest_framework.views import APIView

from goods.models import SKU
from goods.serializers import HostSKUVSerializer

from utils.pagination import StandardResultsSetPagination

# 功能描述:展示热销产品
"""
需求分析:根据前端传回的id返回相应的热销商品种类
GET    /html/categories/(?P<category_id>/d+)/host
"""
#第一种方法
# class HostSKUView(APIView):
#     """
#     接受前端商品id
#     数据库搜索id对应商品
#     序列化
#     返回响应
#     """
#     def get(self,request,category_id):
#         # 接受前端商品id
#         # 数据库搜索id对应商品
#         skus = SKU.objects.filter(category_id=category_id,is_launched=True).order_by('-sales')[:2]
#         # 序列化
#         serializer = HostSKUVSerializer(skus,many=True)
#         # 返回响应
#         return Response(serializer.data)

#第二种方法
class HostSKUView(ListAPIView):

    serializer_class = HostSKUVSerializer
    pagination_class = None

    # queryset = SKU.objects.filter(category_id=category_id,is_launched=True).order_by('-sales')[:2]
    def get_queryset(self):
        category_id = self.kwargs['category_id']
        return SKU.objects.filter(category_id=category_id,is_launched=True).order_by('-sales')[:2]


# 功能描述:展示对应分类列表
"""
需求分析:根据分类展示所有数据\分页\排序
GET     /html/categories/(?P<category_id>\d+)/skus/?page=xxx&page_size=xxx&ordering=xxx
"""
#第一种方法
# class SKUListView(APIView):
#     """
#     接受前端分类数据,
#     根据商品分类id查找对应商品
#     序列化数据
#     返回响应
#     """
#     def get(self,request,category_id):
#         # 接受前端分类数据,
#         # 根据商品分类id查找对应商品
#         skulist = SKU.objects.filter(category_id=category_id,is_launched=True)
#         # 序列化数据
#         serialzier = HostSKUVSerializer(skulist,many=True)
#         # 返回响应
#         return Response(serialzier.data)
class SKUListView(ListAPIView):

    serializer_class = HostSKUVSerializer
    #排序
    filter_backends = [OrderingFilter]
    ordering_fields = ['price', 'sales', 'create_time ']

    pagination_class = StandardResultsSetPagination
    def get_queryset(self):
        category_id = self.kwargs["category_id"]
        skulist = SKU.objects.filter(category_id=category_id,is_launched=True)
        return skulist


from .serializers import SKUIndexSerializer
from drf_haystack.viewsets import HaystackViewSet

class SKUSearchViewSet(HaystackViewSet):
    """
    SKU搜索
    """
    index_models = [SKU]

    serializer_class = SKUIndexSerializer